package csu.web.credit_bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import csu.web.credit_bank.mapper.CreditCertificationsMapper;
import csu.web.credit_bank.pojo.Course;
import csu.web.credit_bank.pojo.CreditCertification;
import csu.web.credit_bank.pojo.dto.CourseCertificationRequestDTO;
import csu.web.credit_bank.service.CoursesService;
import csu.web.credit_bank.service.CreditCertificationsService;
import csu.web.credit_bank.utils.UUIDManager;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class CreditCertificationsServiceImpl extends ServiceImpl<CreditCertificationsMapper, CreditCertification> implements CreditCertificationsService {

    @Autowired
    private UUIDManager uuidManager;
    @Autowired
    private CoursesService coursesService;
    @Override
    public CreditCertification createCertification(CreditCertification certification) {
        String id = uuidManager.getUUID();
        if (id == null) {
            throw new RuntimeException("ID池已空，无法创建认证申请");
        }
        certification.setId(id);
        certification.setAuditStatus(0); // 默认审核中
        this.save(certification);
        return certification;
    }

    @Override
    public CreditCertification getCertificationById(String id) {
        return this.getById(id);
    }

    @Override
    public CreditCertification updateCertification(CreditCertification certification) {
        this.updateById(certification);
        return this.getById(certification.getId());
    }

    @Override
    public boolean deleteCertification(String id) {
        return this.removeById(id);
    }

    @Override
    public CreditCertification createCourseCertification(CourseCertificationRequestDTO requestDTO) {
        // 1. 根据课程名称查找课程信息
        // 由于 coursesService.getOne 需要 QueryWrapper，并且课程名称可能不唯一
        // 这里使用 list 方法，并要求精确匹配或者处理模糊匹配
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", requestDTO.getCourseName());
        // 如果期望只找到一个，可以用 getOne，但如果可能有多个，需要前端提供更精确的信息或这里处理多结果
        // 这里简化为期望找到一个精确匹配的课程
        Course course = coursesService.getOne(queryWrapper);

        if (course == null) {
            throw new IllegalArgumentException("未找到匹配的课程: " + requestDTO.getCourseName());
        }

        // 2. 创建 CreditCertification 对象
        CreditCertification certification = new CreditCertification();
        String id = uuidManager.getUUID();
        if (id == null) {
            throw new RuntimeException("ID池已空，无法创建课程认证申请");
        }
        certification.setId(id);
        certification.setApplicantId(requestDTO.getApplicantId());
        certification.setImageUrl(requestDTO.getImageUrl());
        certification.setAuditStatus(0); // 默认审核中
        certification.setAuditTime(new Date()); // 设置申请时间

        // 3. 将课程认证类型和课程ID、课程学分编码到 description 字段
        // 格式：COURSE_CERT:[courseId]:[courseCredit]:[courseName]:[originalDescription]
        // 这种格式允许我们后续在审核员界面解析并显示课程相关信息
        String encodedDescription = String.format("COURSE_CERT:%s:%d:%s",
                course.getId(),
                course.getCredit(),
                course.getName());
        if (StringUtils.hasText(requestDTO.getDescription())) {
            encodedDescription += ":" + requestDTO.getDescription();
        }
        certification.setDescription(encodedDescription);


        // 4. 保存认证申请
        this.save(certification);
        return certification;
    }
}